<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include/src :: form_base_js_and_css"/>
</head>
<body style="background-color: #f2f2f2; height: 100%;">

<div id="id_panel">
</div>
<script th:inline="javascript">
	// 表单初始化
	var formObj = null;
	//使用layui的sku模块
	layui.config({
		//自定义layui组件的目录,设定扩展的layui模块的所在目录，一般用于外部模块扩展
		base: '/static/js/layui_extends/',
		version: true
	}).use(['form', 'jquery', 'laydate', 'YsForm', 'YsModal'], function(){
		var $ = layui.jquery,
				form = layui.form,
				laydate = layui.laydate,
				YsForm = layui.YsForm,
				YsModal = layui.YsModal;
		/*
		* 表单初始化
		* 参数1：表单配置参数
		* 参数2：子表配置参数
		*/
		formObj = YsForm.init(
				{
					id:'id_panel', // 页面容器ID
					dto: $.parseJSON(JSON.stringify([[${dto}]]))
				}
		);

		/**
		 * 监听兑换方式 TODO 未完善
		 */
		var domPanel = $('#' + formObj.c.id);

		var domForm1 = $('#' + formObj.c.id_form);
		domForm1.find('input[name="isJgSub"]').on('change',function (e) {
			var t = formObj.c.subGridObj;
			var isJgSub = domForm1.find('input[name="isJgSub"]').val();//是否加购子件
			if(isJgSub=='0'){
				for(var i=0;i<t.c.sonlist.length;i++){
					var spAmount = parseFloat(t.c.sonlist[i]['spAmount']);
					t.c.sonlist[i]['spAmount'] = spAmount;

					//计划采购数量 = 采购数量*子件数量
					var purchaseAmount = 0;
					if( spAmount && t.c.sonlist[i]['subAmount']) {
						purchaseAmount = (spAmount * parseFloat(t.c.sonlist[i]['subAmount'])).toFixed(2);
					}
					t.c.sonlist[i]['purchaseAmount'] = purchaseAmount;

					//子件总数 = 计划采购数量+子件加购数量
					var subTotalAmount = purchaseAmount;
					t.c.sonlist[i]['jgSl'] = 0 ;
					t.c.sonlist[i]['subTotalAmount'] = subTotalAmount;

					//子件预估总价 = 子件预估单价*子件总数
					var subTotalPrice = 0;
					if(t.c.sonlist[i]['bprice']){
						subTotalPrice = (subTotalAmount * parseFloat(t.c.sonlist[i]['bprice'])).toFixed(2);
					}
					t.c.sonlist[i]['subTotalPrice'] = subTotalPrice;

				}
				//重新计算采购预估金额
				var totalPrice = 0 ;
				for(var i=0;i<t.c.sonlist.length;i++){
					if(t.c.sonlist[i].subTotalPrice){
						totalPrice = totalPrice+parseFloat(t.c.sonlist[i].subTotalPrice);
					}
				}
				var domForm = $('#' + window.formObj.c.id_form);
				//回填采购预估金额
				domForm.find('input[name="totalPrice"]').val(totalPrice.toFixed(2));
			}



			//formObj.c.subGridObj.reloadGrid();
			var col = formObj.c.subGridObj.listenColByGridId([]);
			layui.table.reload(formObj.c.subGridObj.c.id_table_sub,{cols:col});

		});


		formObj.matchValuesAfterModalSelectRadio=function (colCode, rowSelected, extraData) { // 单选框选中数据后，匹配表单数值
			var t = formObj;
			var domForm = $('#' + t.c.id_form);
			console.log('rowSelected',rowSelected);
			var vgsbm = formObj.getValueByName("vgsbm");
			for(var i = 0; i < t.c.dto.rows.length; i++){
				var col = t.c.dto.rows[i];
				if (col.code == colCode) {
					// 给选择框设置的字段赋值
					domForm.find('input[name="' + colCode + '"]').val(rowSelected[col.modalValueCode]);
				} else if(col.modalRelativeCode == colCode) {
					domForm.find('input[name="' + col.code + '"]').val(rowSelected[col.modalValueCode]);
				}
			}
			var vgsbmByNew = rowSelected.vgsbm?rowSelected.vgsbm:vgsbm;
			console.log('vgsbm',vgsbm);
			console.log('vgsbmByNew',vgsbmByNew);

			if(vgsbm!=vgsbmByNew){
				formObj.c.subGridObj.c.sonlist = [];
				formObj.c.subGridObj.reloadGrid();
			}


			//}
		};



	});


</script>

</body>
</html>